class Startup {

    constructor(basePath) {
        this.basePath = basePath;
        this.argv = require("optimist").argv;
        this.module = {};
        this.lib = {};
    }

    loadConfig(config) {
        try {
            return this.config = require(`${this.basePath}/configs/${this.argv.dev ? 'development' : 'production'}.json`);
        }
        catch(err) {
            
        }
    }

    loadModule(...moduleConf) {
        moduleConf.forEach(conf => {
            let name, moduleName, args
            if(conf instanceof Array && conf.length > 1) {
                name = conf[0];
                moduleName = conf[1];
                if(conf.length == 3)
                    args = conf[2];
            }
            else if(conf)
                name = moduleName = conf || conf[0];
            else
                throw new Error();
            const _name = name[0].toLowerCase() + name.substring(1);
            this.module[`_${_name}`] = new (require(moduleName))(args);
            Object.defineProperty(this.module, _name, {
                get: function() {
                    return this[`_${_name}`];
                }
            });
            this.lib.debug.info('load module:' + name);
        });
        return this.module;
    }

    loadLib(...libConf) {
        libConf.forEach(conf => {
            let name, args;
            if(conf instanceof Array && conf.length == 2) {
                name = conf[0];
                args = conf[1];
            }
            else
                name = conf;
            const _name = name[0].toLowerCase() + name.substring(1);
            this.lib[`_${_name}`] = new (require(`${this.basePath}/libs/${name}`))(args);
            Object.defineProperty(this.lib, _name, {
                get: function() {
                    return this[`_${_name}`];
                }
            });
        });
        return this.lib;
    }

    registerProcessEvent() {
        process.on("uncaughtException", (err) => {
            console.error("【capture unhandled exceptions】");
            console.error(err);
            console.error("【capture unhandled exceptions】");
        });
        process.on('exit', (code) => {
            
        });
    }

}

module.exports = Startup;